<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="http://toy_soldier.gitee.io/huan/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/huan/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/huan/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/huan/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/huan/">Home</a>
        
          <a class="main-nav-link" href="/huan/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/huan/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://toy_soldier.gitee.io/huan"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-Python大作业记录" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/huan/2020/05/08/Python%E5%A4%A7%E4%BD%9C%E4%B8%9A%E8%AE%B0%E5%BD%95/" class="article-date">
  <time datetime="2020-05-08T06:25:31.111Z" itemprop="datePublished">2020-05-08</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/huan/2020/05/08/Python%E5%A4%A7%E4%BD%9C%E4%B8%9A%E8%AE%B0%E5%BD%95/">Python大作业记录</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>五一放了5天，结果大作业一点都没动，我也是十分佩服自己了，泰山崩于前而面不改色。我选的题目是机器学习，之前之跟着李老师的课马马虎虎上了几节，面对着还有两天的deadline，说实话有点发慌了，不管怎么说，作业还是要做的，下面就开始吧。</p>
<h2 id="三个框架的简介和安装"><a href="#三个框架的简介和安装" class="headerlink" title="三个框架的简介和安装"></a>三个框架的简介和安装</h2><p>配置了tensorflow，keras和pytorch.根据我搜的资料，pytorch和tensorflow基本上关系不大，是两个独立的深度学习框架，而Keras和Tensorflow的关系就比较密切了，按照网上的说法，Keras是TensorFlow和Keras的接口（Keras作为前端，TensorFlow或theano作为后端），它也很灵活，且比较容易学。可以把keras看作为tensorflow封装后的一个API。<br><img src="https://img-blog.csdn.net/20171030185127416?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FwZWNhcGU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="keras&amp;tensorflow"></p>
<p>所以安装Keras之前首先要安装Tensorflow，这就让keras的安装比其他两个框架多了一个步骤。Tensorflow和Pytorch的框架都有cpu和gpu两个版本，对于有gpu的机器选择gpu版的应该会提升更高的效率。</p>
<h2 id="使用MNIST数据集"><a href="#使用MNIST数据集" class="headerlink" title="使用MNIST数据集"></a>使用MNIST数据集</h2><p>Pytorch可以直接使用一下代码直接下载使用MNIST数据集，对于网络情况好的开发者十分方便</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 下载训练集</span><br><span class="line">train_dataset &#x3D; datasets.MNIST(root&#x3D;&#39;.&#x2F;num&#x2F;&#39;,     #参数仅供参考</span><br><span class="line">                               train&#x3D;True,</span><br><span class="line">                               transform&#x3D;transforms.ToTensor(),</span><br><span class="line">                               download&#x3D;True)</span><br><span class="line"># 下载测试集</span><br><span class="line">test_dataset &#x3D; datasets.MNIST(root&#x3D;&#39;.&#x2F;num&#x2F;&#39;,      #参数仅供参考</span><br><span class="line">                              train&#x3D;False,</span><br><span class="line">                              transform&#x3D;transforms.ToTensor(),</span><br><span class="line">                              download&#x3D;True)</span><br></pre></td></tr></table></figure>
<p>因为国内某种众所周知的网络原因，直接下载效率很差，查询资料发现需要修改mnist.py文件后才能打开本地的数据集。</p>
<p>Tensorflow也可以直接下载MNIST数据集，相比Pytorch下载更快，但也可以调用函数来直接读取本地MNIST数据集。在这里我虽然比较确信pytorch也有可以调用本地MNIST数据集的API，但绝大多数帖子都采用修改mnist.py文件的方法，所以若要调用本地的MNIST数据集，可能Tensorflow才是更好的选择。</p>
<h2 id="搭建网络"><a href="#搭建网络" class="headerlink" title="搭建网络"></a>搭建网络</h2><p>Pytorch定义卷积层和全连接层时和Tensorflow不太一样，Pytorch需要指定上一层的输出，比如在卷积层需要给出上一层输出的特征层的深度，在全连接层需要指定上一结点输出值的个数（节点个数），在Tensorflow里则不需要这样做，它会自动进行推理，所以在Tensorflow里需要定义的参数比pytorch要少很多。</p>
<p>Tensorflow可以用keras函数搭建网络,也可以类似pytorch用子类模型搭建，相比pytorch的class子类，调用keras的API更加方便，但因为我当时还不熟悉用<code>fit()</code>来执行训练，所以在写Tensorflow的代码时仍然采用了class子类的搭建方式。这里要注意的是pytorch和Tensorflow的padding方法有所区别，前者是手动赋参实现padding，而后者除了可以手动实现，还可以直接选择‘valid‘或’same‘来实现自动padding。除此之外二者基本没有差别。</p>
<h2 id="模型训练"><a href="#模型训练" class="headerlink" title="模型训练"></a>模型训练</h2><p>因为使用pytorch是我首次接触卷积神经网络，对于网络参数等选择并不太好，所以在和同学的正确率比较后发现自己的参数导致最后正确率比较低，看了网上的帖子，发现需要根据经验和尝试不断选择效果更好的参数。因为我使用的是cpu版本，跑一个模型花了将近半小时！也是查找资料后得知，若使用gpu的话会快10~20倍，终于明白了为什么深度学习都使用高级的gpu来训练模型的。</p>
<p>要说明一点的是tensorflow中有一个<code>@tf.fuction</code>的装饰器，它可以吧Python代码转译成高效的tensorflow的图结构，能够在GPU，TPU上运算（具体请参考官方文档，并且根据他人的反馈来看，使用过程的陷阱比较多，但由于本项目比较简单，所以还是尝试使用了该装饰器）。</p>
<p>同时，在pytorch中可以跟踪每一个可训练参数的误差梯度，而tensorflow不会一直去跟踪，所以需要使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">with tf.GradientTape() as tape:</span><br><span class="line">  predictions &#x3D; model(images)</span><br><span class="line">  loss &#x3D; loss_object(labels, predictions)</span><br><span class="line">gradients &#x3D; tape.gradient(loss, model.trainable_variables)</span><br><span class="line">optimizer.apply_gradients(zip(gradients, model.trainable_variables))</span><br></pre></td></tr></table></figure>
<p>来将误差梯度算出来并传递。</p>
<p>在使用相同的网络参数和batch情况下，Tensorflow的耗费时间更少，并且准确率更高。经过和同学交流，发现我们在Tensorflow和pytorch里使用的优化器和损失函数都不同，所以还并不能准确地比较两种框架的效率，但按照我自己使用过程来看，tensorflow相比与pytorch更加方便一点，并且tensorflow将keras作为了内部API，完全可以按照使用keras的方法来搭建神经网络，比pytorch更加方便和高效。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://toy_soldier.gitee.io/huan/2020/05/08/Python%E5%A4%A7%E4%BD%9C%E4%B8%9A%E8%AE%B0%E5%BD%95/" data-id="ck9xurdp5000084ui5kc25u7r" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-起点" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/huan/2020/04/23/%E8%B5%B7%E7%82%B9/" class="article-date">
  <time datetime="2020-04-23T12:58:57.669Z" itemprop="datePublished">2020-04-23</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/huan/2020/04/23/%E8%B5%B7%E7%82%B9/">起点</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>从大创的结题来看，如果是在做一个项目的话，除了写好注释，最好还要写一个实施文档，因为你永远不知道什么时候会有一个2000字在等着你。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://toy_soldier.gitee.io/huan/2020/04/23/%E8%B5%B7%E7%82%B9/" data-id="ck9cs32g90000c4uifkyo21fa" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-hello-world" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/huan/2020/04/23/hello-world/" class="article-date">
  <time datetime="2020-04-23T12:16:29.175Z" itemprop="datePublished">2020-04-23</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/huan/2020/04/23/hello-world/">Hello World</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>

<p>More info: <a href="https://hexo.io/docs/one-command-deployment.html" target="_blank" rel="noopener">Deployment</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://toy_soldier.gitee.io/huan/2020/04/23/hello-world/" data-id="ck9cr9e1n000018uibz2jax7m" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/huan/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/huan/archives/2020/04/">April 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/huan/2020/05/08/Python%E5%A4%A7%E4%BD%9C%E4%B8%9A%E8%AE%B0%E5%BD%95/">Python大作业记录</a>
          </li>
        
          <li>
            <a href="/huan/2020/04/23/%E8%B5%B7%E7%82%B9/">起点</a>
          </li>
        
          <li>
            <a href="/huan/2020/04/23/hello-world/">Hello World</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/huan/" class="mobile-nav-link">Home</a>
  
    <a href="/huan/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/huan/fancybox/jquery.fancybox.css">

  
<script src="/huan/fancybox/jquery.fancybox.pack.js"></script>




<script src="/huan/js/script.js"></script>




  </div>
</body>
</html>